﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Messaging;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Serialization;
using OrdersMessagingPrototype;
using OrdersDAL;
using OrdersDAL.EntityModel;
using log4net;
using log4net.Config;
using Core;

namespace OrdersMessagingPrototype
{
    class Program
    {

        public static readonly ILog logger = LogManager.GetLogger("OrdersPrototype");
        private static string _LoggedUser;
        private static string _QueuePath;
        private static MessagingService _MessagingService;
        private static bool _KeepRunning = true;

        /// <summary>
        /// Service For Creating New Orders and notifies Services to process them
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            BasicConfigurator.Configure();
            
            _LoggedUser = Login();
  
           CreateNewOrderAsync();
        }

        private static void CreateNewOrderAsync()
        {
            _QueuePath = Settings1.Default.MSMQURL + string.Format("\\{0}", _LoggedUser);

            _MessagingService = new MessagingService(_QueuePath);

            var acceptCancelKey = Task.Factory.StartNew(
                () => AcceptCancelAsync());

            Console.WriteLine("New Orders Creation started...\r\nPress Esc to Cancel...");

            while (_KeepRunning)
            {
                Orders order = new Orders();
                order.Status = "N";
                order.Description = "Created By client";
                order.Date_Created = DateTime.Now;
                BL.CreateOrder(order);
                Console.Write("\rCreating new order {0}", order.Id);

                var task = Task.Factory.StartNew(
                    () => SendMessageToQueue(order)); 
                                        
                Thread.Sleep(100);
            }

        }

        
        private static void SendMessageToQueue(Orders order)
        {
            _MessagingService.Send<Orders>(order, Settings1.Default.MessageTTL);
        }

        private static string Login()
        {
            Console.WriteLine("Please Enter User Name:");
            
            string UserName=string.Empty;

            bool ReadUserName = true;

            while (ReadUserName)
            {
                UserName = Console.ReadLine();

                if (string.IsNullOrEmpty(UserName))
                {
                    Console.WriteLine("User Name cannot be empty");
                }
                else
                {
                    ReadUserName = false;
                    Console.WriteLine("Logged as User {0}", UserName);
                }
            }

            return UserName;
        }

        private static void AcceptCancelAsync()
        {
            var key = Console.ReadKey(true);
            while (key.Key != ConsoleKey.Escape)
            {
                key = Console.ReadKey(true);
            }
            _KeepRunning = false;
            Console.WriteLine("Order Receiving was cancelled");
        }
    }
}
